#!/bin/bash
# date: 2024/08/28

# kubectl api-resources | grep apisix
# apisixclusterconfigs              acc          apisix.apache.org/v2                        false        ApisixClusterConfig
# apisixconsumers                   ac           apisix.apache.org/v2                        true         ApisixConsumer
# apisixglobalrules                 agr          apisix.apache.org/v2                        true         ApisixGlobalRule
# apisixpluginconfigs               apc          apisix.apache.org/v2                        true         ApisixPluginConfig
# apisixroutes                      ar           apisix.apache.org/v2                        true         ApisixRoute
# apisixtlses                       atls         apisix.apache.org/v2                        true         ApisixTls
# apisixupstreams                   au           apisix.apache.org/v2                        true         ApisixUpstream

# https://apache.github.io/apisix-helm-chart/
# https://apisix.apache.org/zh/docs/helm-chart/apisix/
# https://github.com/apache/apisix-helm-chart/releases
# https://apisix.apache.org/zh/blog/2021/12/15/deploy-apisix-in-kubernetes/

# apisix-etcd
# move nfs volumes
# - name: data
#   emptyDir: {}

# apisix
# 2024/08/09 09:57:35 [emerg] 1#1: socket() [::]:9080 failed (97: Address family not supported by protocol)
# enable_ipv6: false

# apisix
#   enable_ipv6: false # Enable nginx IPv6 resolver

# apisix
# 403 Forbidden
# allow_admin:
#   # - 127.0.0.1/24

# apisix-ingress-controller
# 2024-08-28T15:58:16+08:00       error   apisix/cluster.go:446   failed to sync schema: Get "http://apisix-admin.apisix.svc.cluster.local:9180/apisix/admin/plugins?all=true": context canceled
# https://github.com/apache/apisix-ingress-controller/issues/1640
# fix: v2 --- v3
#   admin_api_version: "v3"

# apisix core bug
# https://github.com/apache/apisix/issues/11965
# apisix的options请求返回头偶发不返回cors头, 业务无流量变化uat不复现，重启apisix解决

apisix_helm() {

    helm repo add apisix https://charts.apiseven.com
    helm repo update
    # helm search repo apisix -l

    # apisix、etcd

    # helm pull apisix/apisix --version=2.5.0
    # wget https://github.com/apache/apisix-helm-chart/releases/download/apisix-2.5.0/apisix-2.5.0.tgz
    # helm install apisix apisix-2.5.0.tgz --create-namespace --namespace apisix --dry-run

    # helm install apisix apisix/apisix --version=2.5.0 --create-namespace --namespace apisix --dry-run > apisix-2.5.0.yaml
    helm install apisix apisix/apisix --version=2.5.0 --create-namespace --namespace apisix
    # helm uninstall apisix --namespace apisix

    # apisix-dashboard
    # helm pull apisix/apisix-dashboard --version=0.8.2
    # helm install apisix-dashboard apisix-dashboard-0.8.2.tgz --create-namespace --namespace apisix --dry-run

    # helm install apisix-dashboard apisix/apisix-dashboard --version=0.8.2 --namespace apisix --dry-run > apisix-dashboard-0.8.2.yaml
    helm install apisix-dashboard apisix/apisix-dashboard --version=0.8.2 --namespace apisix
    # helm uninstall apisix-dashboard --namespace apisix

    # apisix-ingress-controller
    # helm pull apisix/apisix-ingress-controller --version=0.14.0
    # helm install apisix-ingress-controller apisix-ingress-controller-0.14.0.tgz --create-namespace --namespace apisix --dry-run

    # helm install apisix-ingress-controller apisix/apisix-ingress-controller --version=0.14.0 --namespace apisix --dry-run > apisix-ingress-controller-0.14.0.yaml
    helm install apisix-ingress-controller apisix/apisix-ingress-controller --version=0.14.0 --namespace apisix
    # helm uninstall apisix-ingress-controller --namespace apisix
}

install() {
    kubectl apply -f namespace.yaml
    # helm install apisix apisix --create-namespace --namespace apisix --dry-run > apisix.yaml
    helm install apisix apisix --namespace apisix
    helm install apisix-dashboard apisix-dashboard --namespace apisix
    helm install apisix-ingress-controller apisix-ingress-controller --namespace apisix
}

uninstall() {
    helm -n apisix uninstall apisix
    helm -n apisix uninstall apisix-dashboard
    helm -n apisix uninstall apisix-ingress-controller

    kubectl delete -f namespace.yaml
}

status() {
    kubectl -n apisix get all
    helm -n apisix list
}

case "${1}" in
install)
    install
    ;;
uninstall)
    uninstall
    ;;
status)
    status
    ;;
*)
    echo "$0 install|uninstall|status"
    ;;
esac
